<template>
  <div class="login-container">
    <el-card class="login-box">
      <template #header>
        <h2 class="login-title">玉米产量估测系统V0.1</h2>
      </template>
      
      <el-form 
        :model="loginForm" 
        ref="loginForm"
        :rules="rules"
        label-position="top">
        
        <el-form-item label="用户名" prop="username">
          <el-input 
            v-model="loginForm.username"
            placeholder="请输入用户名">
          </el-input>
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input 
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            show-password>
          </el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            class="login-btn"
            :loading="loading"
            @click="handleLogin">
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>

        <div class="action-links">
          <el-button 
            link 
            @click="$router.push('/register')">
            注册账号
          </el-button>
          <el-button 
            link 
            @click="$router.push('/modify')">
            修改密码
          </el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { ElCard, ElForm, ElFormItem, ElInput, ElButton, ElMessage } from 'element-plus'
import axios from 'axios'

export default {
  name: 'LoginView',
  components: {
    ElCard,
    ElForm,
    ElFormItem,
    ElInput,
    ElButton
  },
  data() {
    return {
      loading: false,
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async handleLogin() {
      try {
        this.loading = true        
        const response = await axios.post(API_URLS.AUTH_LOGIN, {
          username: this.loginForm.username,
          password: this.loginForm.password
        })
        if (response.data) {
          const { code, message, user_id } = response.data;
          if (code === 200) {
            ElMessage.success("登陆"+message)
            // 跳转到主页
            this.$store.commit('setUserId', user_id)
            console.log("[login.vue-handleLogin]: user_id: ",user_id)
            this.$router.push(`/main`);
          } else {
            console.log(response.data.data.message)
            ElMessage.error(response.data.data.message || '登录失败')
          }
        }
      } catch (error) {
        console.error('登录错误:', error)
        ElMessage.error('登录失败: ' + (error.response?.data?.message || '网络错误'))
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('@/assets/images/login-bg.jpg');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  position: relative;
}

.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
}

.login-box {
  width: 400px;
  padding: 20px;
  position: relative;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  color: #303133;
  margin: 0;
  font-size: 24px;
}

.login-btn {
  width: 100%;
  margin-top: 20px;
}

.action-links {
  display: flex;
  justify-content: space-between;
  margin-top: 15px;
  padding: 0 10px;
}

.action-links .el-button {
  padding: 0;
  font-size: 14px;
}

:deep(.el-card__header) {
  padding: 15px;
  border-bottom: 1px solid #ebeef5;
}

:deep(.el-form-item__label) {
  font-weight: 500;
}
</style>
  